import { defineComponent, reactive, ref, computed } from "vue";
import LForm, { LFormItem } from "@/components/public/lForm/lForm";
import styles from "./addInsure.module.less";
import { message } from "ant-design-vue";
import LPopup from "@/components/public/lPopup/lPopup";
import { useStore as useUserStore } from "@/store/user";
import {$groupOrderPeopleInsure} from "@/api/groupOrder/groupOrder";
import { Modal } from "ant-design-vue";
export default defineComponent({
  emits: ["cancel", "confirm"],
  setup(props, { emit, expose }) {
    const userStore = useUserStore();
    const state = reactive({
      order_no:"",
      insurance_order_no:"",
      insure_time:"",
      start_date:"",
      id_no:"",
    } as any);
    const cancelHandler = () => {
      addPopupRef.value.close();
    };
    const confirmHandler = (e:any) => {
      $groupOrderPeopleInsure({
        order_no:state.order_no,
        insurance_order_no:state.insurance_order_no,
        insure_time:state.insure_time,
        start_date:state.start_date,
        id_no:state.id_no,
      }).then((res:any)=>{
        if(res.data.status == -1)
          {
            Modal.confirm({
              title: "提示",
              content: "该人员存在脱保风险,是否承保",
              okText: "确认",
              cancelText: "取消",
              onOk: () => {
                $groupOrderPeopleInsure({
                  order_no:state.order_no,
                  insurance_order_no:state.insurance_order_no,
                  insure_time:state.insure_time,
                  start_date:state.start_date,
                  id_no:state.id_no,
                  allow_loss:1,
                }).then((res:any)=>{
                  message.success("该人员承保成功")      
                  emit("confirm");
                  addPopupRef.value.close();
                }).finally(()=>{
                 })
              },
            });
          }
          else
          {
            message.success("该人员承保成功")      
            emit("confirm");
            addPopupRef.value.close();
          }
      }).finally(()=>{
          e.done();
      })
      
    };
    const addPopupRef = ref(null as any);
    const open = (e: any,id_no:any) => {
       state.insurance_order_no = "";
       state.insure_time = "";
       state.start_date = "";
       state.order_no = e;
       state.id_no = id_no;
      addPopupRef.value.open();
    };
    const close = () => {
      addPopupRef.value.close();
    };
    expose({
      open,
      close,
    });
    const LookVoucher = () => (
      <LForm  btns={["cancel", "confirm"]} v-model={state}    
      onCancel={() => {
        cancelHandler();
      }}
      onConfirm={(e: any) => {
        confirmHandler(e);
      }}>
        <LFormItem label="系统保单号" prop="">
          <a-input disabled v-model:value={state.order_no} placeholder="请输入系统保单号" />
        </LFormItem>
        <LFormItem label="保司保单号" prop="">
          <a-input v-model:value={state.insurance_order_no} placeholder="请输入保司保单号" />
        </LFormItem>
        <LFormItem label="生效时间" prop="">
        <a-date-picker
                onChange={(e: any, string: any) => {
                  state.start_date = string;
                }}
              />
        </LFormItem>
        <LFormItem label="承保时间" prop="">
        <a-date-picker
                onChange={(e: any, string: any) => {
                  state.insure_time = string;
                }}
              />
        </LFormItem>
      </LForm>
    );
    const Components = () => (
      <LPopup title="承保" ref={addPopupRef}>
          <div class={styles.addInsureBox}>
            <LookVoucher/>
          </div>
      </LPopup>
    );
    return {
      Components,
    };
  },
  render() {
    return <this.Components />;
  },
});